/***************************************************************************
 *   Copyright (C) 2008 by Rysiek Król,,,   *
 *   rysiek@Babylon2   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "progressbardialogs.h"
#include <QApplication>
/**
* Constructor class
*/
ProgressBarDialogs::ProgressBarDialogs()
{
}


ProgressBarDialogs::~ProgressBarDialogs()
{
}
/**
* Handles incomming event
*/
void ProgressBarDialogs::attendValue(ProgressDialogEvent * event)
{
        switch (event->getTyp()) {
        case ProgressDialogEvent::CopyFiles:
                this->initializeCopy(event);
                break;
        case ProgressDialogEvent::RemoveComments:
                this->initializeRemoveComments(event);
                break;
        case ProgressDialogEvent::DeleteFiles:
                this->initializeDelete(event);
                break;
        case ProgressDialogEvent::MoveFiles:
                this->initializeMove(event);
                break;
	case ProgressDialogEvent::SaveImageFile:
		this->initializeSaveImageFile(event);
		break;	
        }
}
/**
* Handles progress bar copy action
*/
void ProgressBarDialogs::initializeCopy(ProgressDialogEvent * event)
{
        if (event->getProgressValue()==1) {
                QProgressDialog *progress = new QProgressDialog(tr ("Copying files..."), tr ("Abort Copy"), event->getProgressValue(), event->getFinalCount());
                progress->setWindowModality(Qt::WindowModal);
                DialogItem *item = new DialogItem(progress,event->getHandler());
                this->lista.push_back(item);
                progress->setLabelText(tr ("Coping files: ")+ event->getFileName() );
        } else {
                ItemPair *item = this->findItem(event->getHandler());
                if (item!='\0') {
                        if (!item->second->first->wasCanceled()) {
                                item->second->first->setLabelText(tr ("Coping files: ")+ event->getFileName() );
                                item->second->first->setValue(event->getProgressValue());
                                if (event->getProgressValue()==event->getFinalCount()) {
                                        this->lista.removeAt(item->first);
                                        delete item->second;
                                        delete item;
                                }
                        } else {
                                event->getHandler()->setIsCanceled(true);
                                this->lista.removeAt(item->first);
                                delete item->second;
                                delete item;
                        }
                }
        }
}

/**
* Returns ItemPair witch coresponds to handler parameter
*/
ProgressBarDialogs::ItemPair * ProgressBarDialogs::findItem(FileOperationsHandler * handler)
{
        QListIterator<DialogItem*> iterator(this->lista);
        ItemPair *item = '\0';
        DialogItem *dialogItem;
        int position =0;
        while (iterator.hasNext()) {
                dialogItem = iterator.next();
                if (handler==dialogItem->second) return new ItemPair(position,dialogItem);
                position++;
        }
        return item;
}

/**
* Handles progress bar delete action
*/
void ProgressBarDialogs::initializeDelete(ProgressDialogEvent * event)
{
        if (event->getProgressValue()==1) {
                QProgressDialog *progress = new QProgressDialog(tr ("Deleting files..."), tr ("Abort"), event->getProgressValue(), event->getFinalCount());
                progress->setWindowModality(Qt::WindowModal);
                DialogItem *item = new DialogItem(progress,event->getHandler());
                this->lista.push_back(item);
                progress->setLabelText(tr ("Deleting files: ")+ event->getFileName() );
        } else {
                ItemPair *item = this->findItem(event->getHandler());
                if (item!='\0') {
                        if (!item->second->first->wasCanceled()) {
                                item->second->first->setLabelText(tr ("Deleting files: ")+ event->getFileName() );
                                item->second->first->setValue(event->getProgressValue());
                                if (event->getProgressValue()==event->getFinalCount()) {
                                        this->lista.removeAt(item->first);
                                        delete item->second;
                                        delete item;
                                }
                        } else {
                                event->getHandler()->setIsCanceled(true);
                                this->lista.removeAt(item->first);
                                delete item->second;
                                delete item;
                        }
                }
        }
}

/**
* Handles progress bar for RemoveComments action
*/
void ProgressBarDialogs::initializeRemoveComments(ProgressDialogEvent * event)
{
        if (event->getProgressValue()==1) {
                //QProgressDialog *progress = new QProgressDialog(tr ("Deleting commets..."), tr ("Abort"), event->getProgressValue(), event->getFinalCount());
                QProgressDialog *progress = new QProgressDialog(tr ("Deleting commets..."), NULL, event->getProgressValue(), event->getFinalCount());
                progress->setWindowModality(Qt::WindowModal);
                DialogItem *item = new DialogItem(progress,event->getHandler());
                this->lista.push_back(item);
                progress->setLabelText(tr ("Removing comments from files: ")+ event->getFileName() );
        } else {
                ItemPair *item = this->findItem(event->getHandler());
                if (item!='\0') {
                        item->second->first->setLabelText(tr ("Removing comments from files: ")+ event->getFileName() );
                        item->second->first->setValue(event->getProgressValue());
                        if (event->getProgressValue()==event->getFinalCount()) {
                                this->lista.removeAt(item->first);
                                delete item->second;
                                delete item;
                        }
                }
        }
}
/**
 * Handles progress bar for Move Files action
 */
void ProgressBarDialogs::initializeMove(ProgressDialogEvent * event)
{
        if (event->getProgressValue()==1) {
                QProgressDialog *progress = new QProgressDialog(tr ("Moving files..."), tr ("Abort"), event->getProgressValue(), event->getFinalCount());
                //QProgressDialog *progress = new QProgressDialog(tr ("Moving files..."), NULL, event->getProgressValue(), event->getFinalCount());
                progress->setWindowModality(Qt::WindowModal);
                DialogItem *item = new DialogItem(progress,event->getHandler());
                this->lista.push_back(item);
                progress->setLabelText(tr ("Moving files: ")+ event->getFileName() );
        } else {
                ItemPair *item = this->findItem(event->getHandler());
                if (item!='\0') {
                	if (!item->second->first->wasCanceled()){
                        item->second->first->setLabelText(tr ("Moving files: ")+ event->getFileName() );
                        item->second->first->setValue(event->getProgressValue());
                        if (event->getProgressValue()==event->getFinalCount()) {
                                this->lista.removeAt(item->first);
                                delete item->second;
                                delete item;
                        }
                        } else {
                                //QApplication::postEvent ( event->getHandler(), new QEvent ( QEvent::Type ( ViewerEvent::OperationCanceled ) ) );
                                event->getHandler()->setIsCanceled(true);
                                this->lista.removeAt(item->first);
                                delete item->second;
                                delete item;
                        }
                }
        }
}
/**
 * Handles progress bar for Saving image file action
 */
void ProgressBarDialogs::initializeSaveImageFile(ProgressDialogEvent * event)
{
	// Jeśli jest to początek operacji		
	if (event->getProgressValue()==1) { 
		// Tworzenie progress bara
		QProgressDialog *progress = new QProgressDialog(tr ("Saving Image File..."), NULL, event->getProgressValue(), event->getFinalCount());  
		progress->setWindowModality(Qt::WindowModal); 
		DialogItem *item = new DialogItem(progress,event->getHandler());
		this->lista.push_back(item);
		progress->setLabelText(tr ("Saving Image File: ")+ event->getFileName() );
	} else { 
		// jeśli nie jest to pierwszy event operacji
		ItemPair *item = this->findItem(event->getHandler());
		if (item!='\0') {
			item->second->first->setLabelText(tr ("Saving Image File: ")+ event->getFileName() );
			item->second->first->setValue(event->getProgressValue());
			if (event->getProgressValue()==event->getFinalCount()) {
				this->lista.removeAt(item->first);
				delete item->second;
				delete item;
			}
		}
	}
}




